Preprocessing
Pluto provides some powerful constructs which allow you to write code that will never be seen at runtime.
Function calls
Certain standard library functions can be called at compile-time, as long as the arguments are also known at compile-time, by using the "$" symbol before the function call.
plutolocal runtime = require("crypto").joaat("Hello, World!")local compile = $crypto.joaat("Hello, World!")assert(runtime == compile)
If you're unfamiliar with compile-time evaluation, this is essentially what the code turns into:
plutolocal runtime = require("crypto").joaat("Hello, World!")local compile = 847757641 -- The JOAAT hash of "Hello, World!"assert(runtime == compile)
This is available on preloaded Pluto libraries, such as:
base32base58base64cryptojsonurl
And on the following functions:
io.contentstostringutostringtonumberutonumbertypeassert
Variables
Compile-time constant variables can be defined via the $define statement:
pluto$define a = 123
This may seem identical to a local declared with the const attribute:
plutolocal a <const> = 123
However, $define also enforces that the assigned variable is a compile-time constant:
plutolocal a <const> = {}$define a = {} -- Error: variable was not assigned a compile-time constant value
Conditionals
If there is certain code you only want to have compiled in for a certain build, such as a debug build, you can use compile-time conditionals:
pluto$define DEBUG = true$if DEBUG thenprint("Script running in debug mode")$elseprint("Script running in release mode")$end
In this case, only one of the two paths will be compiled in; the rest will not take up any space.
Aliases
Preprocessor aliases are similar to C/C++ macros. For example, you can define an alias for a keyword:
pluto$alias let = locallet a = 1print(a) --> 1
or write simple functions which will be fully inlined at the call site:
pluto$alias add(a, b) = a + bprint(add(1, 2)) --> 3
If you want to write an alias over multiple lines, you can use a backslash to continue it:
pluto$alias seq = "a" \.. \"b" \.. \"c"print(seq) --> abc
$include
To reuse code for Pluto's parse-time features such as type hinting, you can include a common 'header' file.
header.plutopluto$declare _VERSION: string$declare _PVERSION: string
index.plutopluto$include "header.pluto"local _: number = _VERSION -- type mismatch
$haltcompiler
This statement will stop the parser processing further code.
plutoprint(io.contents("index.pluto"):split("\n$haltcompiler"):back():strip()) --> Anything after the $haltcompiler statement is not processed as Pluto source code.$haltcompilerAnything after the $haltcompiler statement is not processed as Pluto source code.